<template>
  <div class="account-data">
    <div class="add-box">
      <el-button @click="addAccount" type="primary">新增</el-button>
    </div>
    <!-- 数据列表 -->
    <el-table :data="tableData" border style="width:100%" v-loading="loading" element-loading-text="拼命加载中">
      <el-table-column label="服装类型">
        <template slot-scope="scope">
          <span>{{ scope.row.Ftype }}</span>
        </template>
      </el-table-column>

      <el-table-column label="服装价格">
        <template slot-scope="scope">
          <span>{{scope.row.Fmoney}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" size="mini" type="danger">删除</el-button>
          <el-button @click="handleEdit(scope.row)" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pages" ref="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        layout="prev, pager, next"
        :page-size="pages"
        :total="total">
      </el-pagination>
    </div>

    <!-- 新增 -->
    <el-dialog
      title="新增"
      :visible.sync="dialogVisibleAdd"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form :rules="SgradeFrom" ref="AddForm" :model="listObj" class="form-box" >
        <el-form-item label="服装类型" prop="Ftype">
          <el-input v-model="listObj.Ftype" placeholder="请输入服装类型"></el-input>
        </el-form-item>

        <el-form-item label="服装价格" prop="Fmoney">
          <el-input v-model="listObj.Fmoney" placeholder="请输入服装价格"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button @click="handleAdd" type="primary" :loading="loading">确定</el-button>
      </span>
    </el-dialog>


    <!-- 编辑 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisibleEdit"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form :rules="SgradeFrom" ref="EditForm" :model="listObj" class="form-box" >
         <el-form-item label="服装类型" prop="Ftype">
          <el-input v-model="listObj.Ftype" placeholder="请输入服装类型"></el-input>
        </el-form-item>

        <el-form-item label="服装价格" prop="Fmoney">
          <el-input v-model="listObj.Fmoney" placeholder="请输入服装价格"></el-input>
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button @click="handleEditSave()" type="primary" :loading="loading">确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";

@Component({
  components: {  }
})
export default class AccountData extends Vue {
  tableData: any = [];
  total:number = 0; // 总数据量
  pages:number = 5; // 每页数据量
  pageNumber: number = 0; // 页码
  loading:boolean = true;// loading加载
  dialogVisibleAdd: Boolean = false; // 新增模态框显示状态
  dialogVisibleEdit: Boolean = false; // 编辑模态框显示状态
  Fuzhuangid:Number = 0; // 数据ID

  // 对象数据
  listObj: any = {
    Ftype:'', Fmoney:''
  };

  SgradeFrom: any = {
    Ftype: [{ required: true, message: "请输入服装类型", trigger: "blur" }],
    Fmoney: [{ required: true, message: "请输入服装价格", trigger: "blur" }],
  };


  created() {
    this.getData();
  }

  // 新增账户
  handleAdd() {
    (this as any).$refs["AddForm"].validate((valid: boolean) => {
      if (valid) {
        this.loading = true;
        const param:any = this.listObj;

        (this as any).$axios.post("/adminFuzhuang/add", param).then((res: any) => {
          if(res.data.code === 200){
            this.getData();
            this.closeDialog();
            this.loading = false;
            (this as any).$message({
              message: '添加成功',
              type: "success"
            })
          }
          else{
            this.loading = false;
            this.closeDialog();
            (this as any).$message.error(res.data.data)
          }
        })
      }
    });
  }


  // 显示新增账户模态框
  addAccount() {
    this.dialogVisibleAdd = true;
  }

  // 隐藏新增账户
  closeDialog() {
    this.dialogVisibleAdd = false;
    this.dialogVisibleEdit = false;
  }

  // 删除
  handleDelete(row: any): void {
    (this as any).$confirm('是否确认删除？')
    .then((res:any)=> {
      const { Fuzhuangid } = row;

      (this as any).$axios.post('/adminFuzhuang/del', { Fuzhuangid }).then((res: any) => {
        if(res.data.code === 200){
            this.getData();
            (this as any).$message({
              message: res.data.data,
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
      })
    }) 
  }

  handleEdit(row: any){
    this.dialogVisibleEdit = true;
    const { Ftype, Fmoney, Fuzhuangid } = row;
    
    this.listObj.Ftype = Ftype;
    this.listObj.Fmoney = Fmoney;
    this.Fuzhuangid = Fuzhuangid;
  }
  // 编辑保存
  handleEditSave(){
    (this as any).$refs["EditForm"].validate((valid: boolean) => {
      if (valid) {
        this.loading = true;
        const { Ftype, Fmoney } = this.listObj;
        const param:any = {
          Ftype, Fmoney
        };
        param.Fuzhuangid = this.Fuzhuangid;

        (this as any).$axios.post("/adminFuzhuang/update", param).then((res: any) => {
          if(res.data.code === 200){
            this.getData();
            this.closeDialog();
            this.loading = false;
            (this as any).$message({
              message: '修改成功',
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
        })
      }
    });
  }

  // 数据显示
  getData() {
    this.loading = true;
    const param = {
      pages: this.pages,
      pageNumber: this.pageNumber
    };

    (this as any).$axios("/adminFuzhuang/dataList", { params:param }).then((res: any) => {
      this.loading = false;

      if(res.data.code === 200){
        this.tableData = res.data.data.list;
        this.total = res.data.data.sum;
      }
    })
  }

  // 分页 pageSize 改变时会触发
  handleSizeChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }

  // 分页 currentPage 改变时会触发
  handleCurrentChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }
}
</script>

<style lang="scss" scoped>
.account-data {
  height: 100%;
  overflow: auto;
  .add-box {
    margin-bottom: 10px;
  }
}
.pages{
  width: 100%;
  margin-top: 10px;
  background: white;
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}

.form-box {
  .el-input,
  .el-select {
    width: 400px !important;
  }
}
</style>
